package liangtWorkSpace.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName TimeUtils
 * @Description : 功能说明
 * @Author : LiangT
 * @Date : 2020/6/2 12:34
 */
public class TimeUtils {

    /**
     * 使用Calendar对象计算时间差，可以按照需求定制自己的计算逻辑
     * @param strDate
     * @throws ParseException
     */
    public static void calculateTimeDifferenceByCalendar(String strDate)throws ParseException{
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        Date data = format.parse(strDate);
        Calendar c1 = Calendar.getInstance();//获取当前时间
        Calendar c2 = Calendar.getInstance();//获取当前时间
        c2.setTime(data);
        int year = c1.get(Calendar.YEAR);//获取年时间
        int oldYear = c2.get(Calendar.YEAR);//获取年时间

        System.out.println("年差值："+(year-oldYear));
    }

    public static void period (int year, int month, int dayOfMonth){
        LocalDate newLocalDate = LocalDate.now();
        LocalDate oldLocalDate = LocalDate.of(year,month,dayOfMonth);
        Period period = Period.between(oldLocalDate,newLocalDate);
        System.out.println(period.getYears()+"年"+period.getMonths()+"月"+period.getDays()+"天");

    }

    public static void main(String[] args) throws ParseException {
        calculateTimeDifferenceByCalendar("2019/12/14");
        period(2019,12,19);
    }
}
